//
//  BCApiManager.swift
//  BCProject
//
//  Created by you&me on 2017/11/28.
//  Copyright © 2017年 杨杨威. All rights reserved.
//  

import Foundation
import Moya
//NetworkActivityPlugin插件
let networkPlugin = NetworkActivityPlugin { (activityType, targetType) in
    switch activityType {
    case .began:
        BCShowMessage.shareMessage.showTip(msg: "请求中...")
        NSLog("显示loading")
    case .ended:
        BCShowMessage.shareMessage.hidenTip()
        NSLog("隐藏loading")
    }
}

enum BCModuleApi {

    case getMobileCode(mobile: String, destination: String) //短信验证码
    case goLogin(mobile: String, code: String) //登录
    case applyReplenishment(code: String, userId: String, taskNo: Int) //申请补货
    case isRobotOpenRequest(actionType: String, identity: Int)//是否正确接收  开机
//    case requestReplenishment(identity: String) // App确定售卖机是否已经正确接收补货请求
    case doneReplenishment(code: String) //完成补货
//    case finishSupportSuccess(identity: String) //App确定售卖机是否已经正确接收完成补货请求
    case repair(code: String, userId: String, taskNo: Int)//申请维修
//    case submitRepairSuccess(identity: Int)//App确定售卖机是否已经正确接收维修请求
    case RepairCompleteRequest(code: String ,log: String) //完成维修
//    case finishRepairSuccess(identity: String) //App确定售卖机是否已经正确接收完成维修请求
    case active(point: String, province: String, city: String, district: String, street: String, room: String, code: String, mobile: String) //设备激活
    case activeSuccess(code: String) // App确定售卖机是否已经正确接收激活请求
    case unlive(code: String, mobile: String, reason: String)//退服
    case regist(name: String, mobile: String, pMobile: String, code: String)//注册
    case needAuthen(mobile: String)//用户需要审批的列表
    case authenResult(mobile: String, result: String) //审批结果
    case synPositionRequest(mobile: String, position: String, type: String) //同步位置
    case acceptTask(actionType: String,mobile: String, no: Int)//接受任务 / 拒绝任务
}

extension BCModuleApi: TargetType {

    //服务器地址
    var baseURL: URL {
        return URL.init(string: "http://m.highness-cn.com/xpas/apperp.do")!
    }
     //各个请求的具体路径
    var path: String {
        return ""
    }
    //请求类型
    var method: Moya.Method {
        return .post
    }
    /*Alamofire中是这样使用的:

     URLEncoding 和URL相关的编码，有两种编码方式：
     直接拼接到URL中
     通过request的httpBody传值
     JSONEncoding 把参数字典编码成JSONData后赋值给request的httpBody

     PropertyListEncoding把参数字典编码成PlistData后赋值给request的httpBody
     */

    //请求任务事件（这里附带上参数）
    var task: Task {
        switch self {
        case .getMobileCode(let mobile, let destination):
            return .requestParameters(parameters: ["actionType": "getAuthenCode", "mobile": mobile, "destination": destination], encoding: JSONEncoding.default)
        case .goLogin(let mobile, let code):
            return .requestParameters(parameters: ["actionType": "login", "mobile": mobile, "code": code], encoding: JSONEncoding.default)
        case .applyReplenishment(let code, let userId, let taskNo):
            return .requestParameters(parameters: ["actionType": "support","code": code, "userId": userId, "taskNo": taskNo], encoding: JSONEncoding.default)

        case .doneReplenishment(let code):
            return .requestParameters(parameters: ["actionType": "ReplenishmentCompleteRequest", "code": code], encoding: JSONEncoding.default)
        case .repair(let code, let userId, let taskNo):
            return .requestParameters(parameters: ["actionType": "repair", "code": code, "userId": userId, "task": taskNo], encoding: JSONEncoding.default)

        case .RepairCompleteRequest(let code, let log):
            return .requestParameters(parameters: ["actionType": "RepairCompleteRequest", "code": code, "log":log], encoding: JSONEncoding.default )

        case .active(let point, let province, let city, let district, let street, let room, let code, let mobile):
            return .requestParameters(parameters: ["actionType": "active", "point": point, "province": province, "city": city, "district": district, "street": street, "room": room, "code": code, "mobile":  mobile], encoding: JSONEncoding.default)

        case .unlive(let code, let mobile, let reason):
            return .requestParameters(parameters: ["actionType": "unlive", "code": code, "mobile": mobile, "reason": reason], encoding: JSONEncoding.default)
        case .regist(let name, let mobile, let pMobile, let code):
            return .requestParameters(parameters: ["actionType": "regist", "name": name, "mobile": mobile, "pMobile": pMobile, "code": code], encoding: JSONEncoding.default)
        case .needAuthen(let mobile):
            return .requestParameters(parameters: ["actionType": "needAuthen", "mobile": mobile], encoding: JSONEncoding.default)
        case .authenResult(let mobile, let result):
            return .requestParameters(parameters: ["actionType": "authenResult", "mobile": mobile, "result": result], encoding: JSONEncoding.default)
        case .synPositionRequest(let mobile, let position , let type):
            return .requestParameters(parameters: ["actionType": "synPositionRequest", "mobile": mobile, "position": position, "type": type], encoding: JSONEncoding.default)
        case .isRobotOpenRequest(let actionType, let identity):
            return .requestParameters(parameters: ["actionType": actionType, "identity": identity], encoding: JSONEncoding.default)
        case .acceptTask(let actionType, let mobile, let no):
            return .requestParameters(parameters: ["actionType": actionType, "mobile": mobile, "no": no], encoding: JSONEncoding.default)
        case .activeSuccess(let code):
            return .requestParameters(parameters: ["actionType": "activeSuccess", "code": code], encoding: JSONEncoding.default)
        }
    }


    //各个请求的参数 （方法废弃）
    var parameters: [String: Any]? {
        return nil
    }
    //这个就是做单元测试模拟的数据，只会在单元测试文件中有作用
    var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }

    //是否执行Alamofire验证
    public var validate: Bool {
        return false
    }
    //请求头 以JSONEncoding.default  形式的请求  JSONEncoding类型创建了一个参数对象的JOSN展示，并作为请求体。编码请求的请求头的Content-Type请求字段被设置为application/json; charset=utf-8
    var headers: [String : String]? {
        let headers: HTTPHeaders = [
           "Content-Type": "application/json; charset=utf-8"
        ]
        return headers
    }
}

